<template>
  <el-drawer v-bind="$attrs" ref="tableFormRef" @open="openDrawer" @close="closeDrawer" destroy-on-close :close-on-click-modal="false">
    <template #header>
      <h3>{{ formMode == 'add' ? '新增' : '编辑' }}</h3>
    </template>
    <template #default>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120" label-position="left">
        <el-form-item prop="name" label="权限标识">
          <el-input v-model="formData.name" placeholder="请输入权限标识" clearable />
        </el-form-item>
        <el-form-item prop="label" label="权限名称">
          <el-input v-model="formData.label" placeholder="请输入权限名称" clearable />
        </el-form-item>
        <el-form-item prop="parent" label="父级标识">
          <el-input v-model="formData.parent" placeholder="父级标识自动识别" disabled clearable />
        </el-form-item>
        <el-form-item prop="description" label="描述">
          <el-input v-model="formData.description" placeholder="请输入描述" type="textarea" :autosize="{ minRows: 2 }" />
        </el-form-item>
        <el-form-item prop="remark" label="备注">
          <el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" :autosize="{ minRows: 4 }" />
        </el-form-item>
        <el-form-item prop="sort" label="排序">
          <el-input-number v-model="formData.sort" :min="0" :step="1" step-strictly />
        </el-form-item>
        <el-form-item prop="status" label="状态">
          <el-switch
            v-model="formData.status"
            inline-prompt
            :active-value="1"
            :inactive-value="0"
            active-icon="Check"
            inactive-icon="Close"
          />
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="confirm">确认</el-button>
    </template>
  </el-drawer>
</template>

<script setup>
import { ref, watch } from 'vue'
import { ElNotification } from 'element-plus'
import { cloneDeep, isEqual } from 'lodash-es'
import { assignOverrideWithHas } from '@/utils'

const props = defineProps({
  formInit: {
    type: Object,
    default: {}
  },
  formMode: {
    type: String,
    default: 'add'
  }
})

const emits = defineEmits(['submit'])

// 初始数据
const formBase = {
  id: '', // 主键
  name: '',
  label: '',
  parent: '',
  description: '',
  remark: '',
  sort: 0,
  status: 1
}
// 表单数据
const formData = ref(formBase)
// 初始数据克隆
const formBaseClone = ref()
// 校验规则
const rules = ref({
  name: [{ required: true, message: '请输入权限标识', trigger: 'blur' }],
  label: [{ required: true, message: '请输入权限名称', trigger: 'blur' }]
})

// 父级标识自动识别
watch(
  () => formData.value.name,
  (newVal) => {
    formData.value.parent = newVal.split(':')[0]
  }
)

const tableFormRef = ref() // 抽屉
const formRef = ref() // 表单

// 抽屉打开回调
function openDrawer() {
  // 表单数据初始化更新
  formData.value = assignOverrideWithHas(cloneDeep(formBase), props.formInit)
  /**
   * 克隆一个初始数据
   */
  formBaseClone.value = cloneDeep(formData.value)
}

// 抽屉关闭回调
function closeDrawer() {}

// 关闭抽屉
function cancel() {
  tableFormRef.value.handleClose()
}
// 确认提交表单
function confirm() {
  formRef.value.validate(async (valid, fields) => {
    if (valid) {
      // 对比数据是否发生变化
      if (isEqual(formBaseClone.value, formData.value)) {
        // 未变化则提示并关闭抽屉
        ElNotification({
          title: 'Info',
          message: '数据未变更',
          type: 'info'
        })
        tableFormRef.value.handleClose()
        return
      }

      emits('submit', { data: formData.value, mode: props.formMode })
      // tableFormRef.value.handleClose()
    } else {
      console.log('==== 校验失败 :', fields)
    }
  })
}
</script>

<style lang="scss"></style>
